﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LL.Business;
using NanoERP.Model;
using NanoERP.Common;

namespace NanoERP.Business.BaseInfo
{
    /// <summary>
    ///往来单位信息业务类
    /// </summary>
    public class ClientinfoBLL : ServiceBase
    {
        /// <summary>
        /// 往来单位信息查询
        /// </summary>
        /// <param name="clientType">单位类别ID</param>
        /// <param name="query">查询关键字 支持：名称|编码|拼音码|地址|联系人|电话</param>
        /// <returns></returns>
        public List<Clientinfo> Query(long clientType, string query)
        {
            var sql = string.Format(@"{0} ('{1}'='' OR CLIENTNAME like '%{1}%' OR CLIENTNO like '%{1}%'
                                      OR CLIENTPYM  like '%{1}%' OR ADDRESS like '%{1}%' OR LINKMAN like '%{1}%'
                                      OR TELEPHONE like '%{1}%' )", query.Length == 0 ? "(@0=10 OR CLIENTTYPE=@0) AND" : "", query);

            var list = DbSession.Query<Clientinfo>(sql, clientType).ToList();

            var userbll = new UserinfoBLL();
            var deparbll = new DeptinfoBLL();
            list.ForEach(r =>
            {
                if (r.Salesman != null)
                {
                    var obj = userbll.Get(r.Salesman.Value);
                    r.SalesmanName = obj != null ? obj.Username : "";
                }
                if (r.Deptid != null)
                {
                    var obj = deparbll.Get(r.Deptid.Value);
                    r.DeptName = obj != null ? obj.Deptname : "";
                }
            });
            return list;
        }

        /// <summary>
        /// 往来单位信息
        /// </summary>
        /// <param name="id">往来单位信息ID</param>
        /// <returns></returns>
        public Clientinfo Get(long id)
        {
            var obj = DbSession.Get<Clientinfo>(id);
            if (obj!=null&&obj.Salesman != null)
            {
                var user = new UserinfoBLL().Get(obj.Salesman.Value);
                obj.SalesmanName = user != null ? user.Username : "";
            }
            if (obj != null && obj.Deptid != null)
            {
                var dept = new DeptinfoBLL().Get(obj.Deptid.Value);
                obj.DeptName = dept != null ? dept.Deptname : "";
            }

            return obj;
        }

        /// <summary>
        /// 编辑往来单位信息
        /// </summary>
        /// <param name="o"></param>
        public void Edit(Clientinfo o)
        {
            if (o.Id == 0)
                DbSession.Insert(o, "Id", "SalesmanName", "DeptName");
            else
                DbSession.Update(o);
        }

        /// <summary>
        /// 删除往来单位信息
        /// </summary>
        /// <param name="id"></param>
        public void Delete(long id)
        {
            DbSession.Delete<Clientinfo>(id);
        }

        /// <summary>
        /// 浏览记录 上一条 下一条
        /// </summary>
        /// <param name="id">浏览记录Id</param>
        /// <param name="type">类型 0-向上,1-向下</param>
        /// <param name="isLast">是否最后一条数据</param>
        /// <returns></returns>
        public Clientinfo PreRecord(long id, int type, ref bool isLast)
        {
            var obj = DbSession.Get<Clientinfo>(id);
            if (obj == null)
            {
                var dt = DbSession.ExcuteQuery(string.Format("select {0}(ID) as ID from dbo.Clientinfo", type.Equals(0) ? "MIN" : "MAX"));
                var lId = dt.Rows.Count > 0 ? Normal.ToLong(dt.Rows[0][0]) : 0;

                isLast = type.Equals(0) ? id <= lId + 1 : id >= lId - 1;

                var list = DbSession.SqlQuery<Clientinfo>(
                    string.Format("select top 1 * from dbo.Clientinfo where ID {1} {0} Order by Id {2}",
                    isLast ? lId + (type.Equals(0) ? 1 : -1) : id, type.Equals(0) ? "<" : ">", type.Equals(0) ? "desc" : "asc"));

                obj = list.First();
            }

            return obj;
        }
    }
}
